package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.validated.SaveValidated;
import com.info33.platform.common.validated.UpdateValidated;
import com.info33.platform.system.dto.SysPostDTO;
import com.info33.platform.system.entity.SysPost;
import com.info33.platform.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 原系统中的角色
 *
 * @author admin
 */
@RestController
@RequestMapping("/SysPost")
@Api(tags = "系统 - 岗位")
public class SysPostController extends BaseController<SysPostService, SysPost> {

    private final SysPostService sysPostService;

    @Autowired
    public SysPostController(SysPostService sysPostService) {
        super(sysPostService);
        this.sysPostService = sysPostService;
    }

    @GetMapping(value = "/listNoDefault")
    @ApiOperation(value = "获取系统岗位列表", notes = "不包括管理员岗位")
    public Result<List<SysPost>> listNoDefault() {
        return Result.ok(sysPostService.list(new QueryWrapper<SysPost>()
                .eq(TableConstant.TENANT_ID, getTenantId())
                .apply("indexes IS NULL")));
    }

    
    @PostMapping(value = "/role/list")
    @ApiOperation(value = "pc - 获取系统岗位列表", notes = "根据入参 id 英文逗号分割，进行查询")
    public Result<List<SysPost>> roleList(
            @Validated(value = SysPostDTO.RoleList.class) @RequestBody SysPostDTO dto) {
        return Result.ok(sysPostService.list(new QueryWrapper<SysPost>()
                .eq(TableConstant.TENANT_ID, getTenantId())
                .apply("FIND_IN_SET(id, '" + dto.getPostIds() + "') > 0")));
    }

    @PostMapping(value = "/pc/add")
    @ApiOperation(value = "pc - 新增岗位")
    public Result<Object> pcAdd(
            @Validated(value = SaveValidated.class) @RequestBody SysPost entity) {
        return sysPostService.pcAdd(entity, getCurrentUser());
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc - 编辑岗位")
    public Result<Object> pcEdit(
            @Validated(value = UpdateValidated.class) @RequestBody SysPost entity) {
        return sysPostService.pcEdit(entity, getCurrentUser());
    }

    @DeleteMapping(value = "/remove/list")
    @ApiOperation(value = "pc - 批删除")
    public Result<Object> removeList(
            @RequestBody List<Long> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            return Result.error("请选择要删除的岗位");
        }
        return sysPostService.removeList(ids, getCurrentUser());
    }

}